Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@lezer/javascript

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lezer/javascript

lezer-based JavaScript grammar

  • 0.15.3
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is @lezer/javascript?

@lezer/javascript is a parser for JavaScript written using the Lezer parser system. It provides a way to parse JavaScript code into a syntax tree, which can be used for various purposes such as syntax highlighting, code analysis, and transformation.

What are @lezer/javascript's main functionalities?

Parsing JavaScript Code

This feature allows you to parse JavaScript code into a syntax tree. The code sample demonstrates how to parse a simple JavaScript statement and print the resulting syntax tree.

const {parser} = require('@lezer/javascript');
const input = 'const x = 10;';
const tree = parser.parse(input);
console.log(tree.toString());

Syntax Tree Traversal

This feature allows you to traverse the syntax tree. The code sample shows how to use a TreeCursor to iterate over the nodes in the syntax tree and print their types.

const {parser} = require('@lezer/javascript');
const {TreeCursor} = require('@lezer/common');
const input = 'const x = 10;';
const tree = parser.parse(input);
let cursor = tree.cursor();
do {
  console.log(cursor.node.type.name);
} while (cursor.next());

Custom Syntax Highlighting

This feature allows you to apply custom syntax highlighting to JavaScript code. The code sample demonstrates how to define a highlighting style and apply it to a parsed syntax tree.

const {parser} = require('@lezer/javascript');
const {highlightTree} = require('@lezer/highlight');
const {styleTags, tags} = require('@lezer/highlight');
const input = 'const x = 10;';
const tree = parser.parse(input);
const highlightStyle = styleTags({
  'VariableName': tags.variableName,
  'Keyword': tags.keyword,
  'Number': tags.number
});
let highlighted = '';
highlightTree(tree, highlightStyle, (from, to, classes) => {
  highlighted += input.slice(from, to) + ' [' + classes + '] ';
});
console.log(highlighted);

Other packages similar to @lezer/javascript

FAQs

Package last updated on 26 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc